package cn.hzking.gjmall.activitys.my;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.lljjcoder.style.citythreelist.CityBean;
import com.lljjcoder.style.citythreelist.ProvinceActivity;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.hzking.gjmall.R;
import cn.hzking.gjmall.base.ColorStatuBarActivity;
import cn.hzking.gjmall.net.RetrofitFactory;
import cn.hzking.gjmall.net.base.BaseEntity;
import cn.hzking.gjmall.net.base.BaseObserver;
import cn.hzking.gjmall.utils.OnClickEvent;
import cn.hzking.gjmall.utils.UIUtils;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class AddAdressActivity extends ColorStatuBarActivity {
    @Bind(R.id.et_city) EditText cityEt;
    @Bind(R.id.rl_city) RelativeLayout cityRl;
    @Bind(R.id.rl_back) RelativeLayout backRl;
    @Bind(R.id.rl_save) RelativeLayout saveRl;
    @Bind(R.id.et_street) EditText streetEt;
    @Bind(R.id.et_name) EditText nameEt;
    @Bind(R.id.et_tel) EditText telEt;
    private String name;
    private String tel;
    private String street;
    private String provinceName;
    private String cityName;
    private String districtName;
    private String provinceId;
    private String cityId;
    private String districtId;

    //private String address;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_add_adress);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
        if(getIntent()!=null){
            if(!TextUtils.isEmpty(getIntent().getStringExtra("name"))){
                name=getIntent().getStringExtra("name");
                nameEt.setText(name);
            }
            if(!TextUtils.isEmpty(getIntent().getStringExtra("tel"))){
                tel=getIntent().getStringExtra("tel");
                telEt.setText(tel);
            }
            if(!TextUtils.isEmpty(getIntent().getStringExtra("street"))){
                street=getIntent().getStringExtra("street");
                streetEt.setText(street);
            }
            if(!TextUtils.isEmpty(getIntent().getStringExtra("provinceName"))){
                provinceName=getIntent().getStringExtra("provinceName");
            }
            if(!TextUtils.isEmpty(getIntent().getStringExtra("cityName"))){
                cityName=getIntent().getStringExtra("cityName");
            }
            if(!TextUtils.isEmpty(getIntent().getStringExtra("districtName"))){
                districtName=getIntent().getStringExtra("districtName");
                cityEt.setText(provinceName+""+cityName+""+districtName);
            }
            if(!TextUtils.isEmpty(getIntent().getStringExtra("provinceId"))){
                provinceId=getIntent().getStringExtra("provinceId");
            }
            if(!TextUtils.isEmpty(getIntent().getStringExtra("cityId"))){
                cityId=getIntent().getStringExtra("cityId");
            }
            if(!TextUtils.isEmpty(getIntent().getStringExtra("districtId"))){
                districtId=getIntent().getStringExtra("districtId");
            }
        }
        init();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ProvinceActivity.RESULT_DATA) {
            if (resultCode == RESULT_OK) {
                if (data == null) {
                    return;
                }
                //省份结果
                CityBean province = data.getParcelableExtra("province");
                //城市结果
                CityBean city = data.getParcelableExtra("city");
                //区域结果
                CityBean area = data.getParcelableExtra("area");
                provinceName=province.getName();
                cityName=city.getName();
                districtName=area.getName();
                provinceId=province.getId();
                districtId=area.getId();
                cityId=city.getId();
                cityEt.setText(province.getName() +city.getName()+area.getName());
            }
        }
    }

    private void init() {
        cityEt.setCursorVisible(false);
        cityEt.setFocusable(false);
        cityEt.setFocusableInTouchMode(false);
        backRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        cityEt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AddAdressActivity.this, ProvinceActivity.class);
                startActivityForResult(intent, ProvinceActivity.RESULT_DATA);
            }
        });
        saveRl.setOnClickListener(new OnClickEvent() {
            @Override
            public void singleClick(View v) {
                if(TextUtils.isEmpty(cityEt.getText().toString())||TextUtils.isEmpty(streetEt.getText().toString())
                        ||TextUtils.isEmpty(nameEt.getText().toString())||TextUtils.isEmpty(telEt.getText().toString())){
                    UIUtils.showToastShort(AddAdressActivity.this,"请填写完整的信息");
                }else {
                    save();
                }
            }
        });
    }

    private void save() {
        HashMap<String,String> param=new HashMap<>();
        param.put("provinceName",provinceName);
        param.put("cityName",cityName);
        param.put("districtName",districtName);
        param.put("streetAddress",streetEt.getText().toString());
        param.put("receiverName",nameEt.getText().toString());
        param.put("receiverTel",telEt.getText().toString());
        param.put("provinceId",provinceId);
        param.put("cityId",cityId);
        param.put("districtId",districtId);
        /*Log.e("---","provinceName="+provinceName+"cityName="+cityName+"districtName="+districtName+"streetAddress="+
                streetEt.getText().toString()+"receiverName="+nameEt.getText().toString()+"receiverTel"+telEt.getText().toString()+
        "provinceId="+provinceId+"cityId="+cityId+"districtId="+districtId);*/
        RetrofitFactory.getInstant().API()
                .saveAddrsss(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseEntity>(this,true) {

                    @Override
                    protected void onSuccees(BaseEntity baseEntity) throws Exception {
                        Intent intent = new Intent();
                        AddAdressActivity.this.setResult(RESULT_OK, intent);
                        AddAdressActivity.this.finish();
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        if(isNetWorkError){
                            UIUtils.showToastShort(AddAdressActivity.this,getResources().getString(R.string.net_error));
                        }else {
                            UIUtils.showToastShort(AddAdressActivity.this,"服务器错误");
                        }
                    }

                    @Override
                    protected void onCodeError(BaseEntity baseEntity) throws Exception {
                        super.onCodeError(baseEntity);
                        UIUtils.showToastShort(AddAdressActivity.this,baseEntity.getErrorMsg());
                    }
                });
    }
}
